package JUC.lock;

import java.util.concurrent.atomic.AtomicReference;

//自旋锁
public class SpinlockDemo {

    //原子引用
    AtomicReference atomicReference=new AtomicReference<>();
    //加锁
    public void mylock(){
        Thread thread = Thread.currentThread();
        System.out.println(thread.getName()+"==>mylock");

        //CAS比较  自旋锁 当线程是空的，扔进去线程
        while (!atomicReference.compareAndSet(null,thread)){

        }
    }

    //解锁
    public void myUnlock(){
        Thread thread=Thread.currentThread();
        System.out.println(Thread.currentThread().getName()+"==>myUnlock");
        atomicReference.compareAndSet(thread,null);
    }

}
